#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
    ListNode* cur = list1;
    for (int i = 0; i < a - 1; i++)
        cur = cur->next;
    ListNode* prev = cur;
    for (int i = 0; i < b - a + 1; i++)
        cur = cur->next;
    ListNode* end = cur->next;
    prev->next = list2;

    cur = list2;
    while (cur->next != nullptr)
        cur = cur->next;
    cur->next = end;
    return list1;
}